package com.xuecheng.ucenter.service;

import com.xuecheng.framework.domain.ucenter.XcCompanyUser;
import com.xuecheng.framework.domain.ucenter.XcMenu;
import com.xuecheng.framework.domain.ucenter.XcUser;
import com.xuecheng.framework.domain.ucenter.ext.XcUserExt;
import com.xuecheng.ucenter.dao.XcCompanyUserRepository;
import com.xuecheng.ucenter.dao.XcMenuMapper;
import com.xuecheng.ucenter.dao.XcUserRepository;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Aniymx on 2019/1/12 17:59.
 */
@Service
public class UcenterService {
    @Autowired
    private XcCompanyUserRepository xcCompanyUserRepository;
    @Autowired
    private XcUserRepository xcUserRepository;
    @Autowired
    private XcMenuMapper xcMenuMapper;

    /*根据用户名进行查询用户的信息*/
    public XcUserExt getXcUserExt(String username) {
        XcUser xcUser = this.findByUsername(username);
        if (xcUser == null) {
            return null;
        }
        XcUserExt xcUserExt = new XcUserExt();
        BeanUtils.copyProperties(xcUser, xcUserExt);
        String userId = xcUserExt.getId();
        XcCompanyUser xcCompanyUser = xcCompanyUserRepository.findByUserId(userId);
        if (xcCompanyUser != null) {
            xcUserExt.setCompanyId(xcCompanyUser.getCompanyId());
        }
        List<XcMenu> xcMenus = xcMenuMapper.selectPermissionByUserId(userId);
        if (xcMenus != null) {
            xcUserExt.setPermissions(xcMenus);
        }
        return xcUserExt;
    }

    /*根据用户名进行查询用户的信息*/
    private XcUser findByUsername(String username) {
        XcUser byUsername = xcUserRepository.findByUsername(username);
        return byUsername;
    }


    /*public XcCompanyUser findByUserId(String userId) {
        XcCompanyUser xcCompanyUser = xcCompanyUserRepository.findByUserId(userId);
        return xcCompanyUser;
    }*/
}
